package com.tmpt.utils.rsa;

import java.net.URLDecoder;
import java.security.KeyPair;

public class DecodeAction {
	private String keyPairLocation ;
	public DecodeAction(String keyPairLocation) {
		this.keyPairLocation = keyPairLocation;
	}
	public DecodeAction() {}
	
	public String execute(String result) throws Exception {
		//.out.println("=================================");
		String pwd ;
		//.out.println("原文加密后为：");
		//.out.println(result);
		byte[] en_result = RSAUtil.hex2byte(result.getBytes());//new BigInteger(result, 16).toByteArray();
		//.out.println("转成byte[]" + new String(en_result));
		KeyPair keyPair = null;
		if(this.keyPairLocation==null || this.keyPairLocation.equals("")){
			keyPair = RSAUtil.getKeyPairClasspath("rsa.key");
		}else{
			keyPair = RSAUtil.getKeyPair(keyPairLocation);
		}
		byte[] de_result = RSAUtil.decrypt(keyPair.getPrivate(),
				en_result);
		StringBuffer sb = new StringBuffer();
		sb.append(new String(de_result));
		pwd = sb.reverse().toString();
		pwd = URLDecoder.decode(pwd,"UTF-8");//
		//.out.println("还原密文：");
		//.out.println(pwd);
		//.out.println("=================================");
		return pwd;
	}
	
	public static void main(String[] args) throws Exception{
		String result  = "848adc42e24f7c4e881b2697ae894bc228f3bb62c6488a320269c38b0aa3100370b9383c06387fd3f0d0dcd06c00f3f7edbad3e2db56d24b3740ae8b92cbbd631527e74506182704b36da50f0a16af5a272d3972a993842b0e9365b0ae2d14a110ac838406eca178d1a4b14c950fc3a6915a7311c57b4197ecca3e8bd4ff45f2";
		String result3 = "848ADC42E24F7C4E881B2697AE894BC228F3BB62C6488A320269C38B0AA3100370B9383C06387FD3F0D0DCD06C00F3F7EDBAD3E2DB56D24B3740AE8B92CBBD631527E74506182704B36DA50F0A16AF5A272D3972A993842B0E9365B0AE2D14A110AC838406ECA178D1A4B14C950FC3A6915A7311C57B4197ECCA3E8BD4FF45F2";
		String result1 = "1e46e7946dfd475dac21a3b547d377e676ea2d2b07f9111b36b240e6d0d31d2afe24ed29b5235e610b923406adc579b6714eb91388f38d824dbfd1d5255a5395512c4b3a689ccf67dc0e8fc6deef133d496fc612a2f6298452399710739af72e9d275232485f979d7f9065b8860bc50b7b714a8cc841e88a864c9d02cfd98e8f";
		String result2 = "4EED822143C0EFCD40414C819C1B38710C88C82539F798596F607743C0E12EBBA099EB4462D2D6C4DCBBD21FC42CBF2FA77264D00475A04E3E5B4A986FFEA1F6712B4482F9CE3F805C97005FDD3F274B5735A5DABC5EA2BA7187DBB2C964A0BC256FA0CE26933526AC29E4BE2F7C1BD42E3DEE5E7095B86237D4EB85B3D9DB58";
		new DecodeAction().execute(result);
	}
}